package cn.kgc.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>添加描述信息</p>
 *
 * @author 卢传琪
 */
@Configuration
public class RabbitMQConfig {
    @Bean
    public Exchange stockExchange(){
        return new TopicExchange("stock.exchange",true,false);
    }

    @Bean
    public Queue stockDelayQueue(){
        Map<String, Object> arguments=new HashMap<>();
        //设置队列的过期时间
        arguments.put("x-message-ttl",120000);
        //设置死信交换机
        arguments.put("x-dead-letter-exchange","stock.exchange");
        //设置死信路由键
        arguments.put("x-dead-letter-routing-key","unlock.stock");

        Queue queue= new Queue("stock.delay.queue",true,true,false,arguments);
        return queue;
    }

    @Bean
    public  Queue stockUnlockQueue(){
        return new Queue("stock.unlock.queue",true,true,false);
    }

    @Bean
    public Binding stockDelayQueueBinding(){
        //如果向交换机中发送一个信息，如果rountingKey是stock.locked
        //此信息就会进入到stock.delay.queue队列中
        return new Binding("stock.delay.queue",Binding.DestinationType.QUEUE,
                "stock.exchange","lock.stock",null);
    }

    @Bean
    public Binding stockUnlockQueueBinding(){
        //如果向交换机中发送一个信息，如果rountingKey是unlock.stock
        //此信息就会进入到stock.release.queue队列中
        return new Binding("stock.unlock.queue",Binding.DestinationType.QUEUE,
                "stock.exchange","unlock.stock",null);
    }

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

}
